<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\agency\controller;

use app\agency\service\NodeService;
use library\tools\Data;
use think\App;
use think\Console;
use think\Db;
use think\exception\HttpResponseException;
use app\common\facade\TransItem;
use app\common\facade\Arr;

/**
 * 二手报表
 * Class Index
 * @package app\admin\controller
 */
class Marketstat extends AgentBase
{
    public $table = 'Market';
    /**
     * 二手统计
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {

        $this->title="二手统计";
       //获取当前代理商所在城市
        $user=$this->agentUser;
        $data['provinces_id']=$user['provinces_id'];
        $data['city_id']=$user['city_id'];

       
        $result['total_market']=DB::name($this->table)->where($data)->count();
        $result['sucess_market']=DB::name($this->table)->where($data)->where('status=1 ')->count();
        $result['pre_market']=DB::name($this->table)->where($data)->where('status=0')->count();
        $result['deny_market']=DB::name($this->table)->where($data)->where('status=2')->count();
        $this->assign('result',$result);

        $start_time= $this->request->param('start_time');
        $end_time=$this->request->param('end_time');

        if(!empty($start_time)&&!empty($end_time)){

            $start=strtotime($start_time." 00:00:00");
            $end=strtotime($end_time. " 23:59:59");
            if($start>$end){
                $this->error('开始时间应小于结束时间');
            }

        }else{
            $today=date("Y-m-d");
            $start=strtotime($today." 00:00:00");
            $end=strtotime($today. " 23:59:59");
        }
        $st[]=['provinces_id','EQ',$user['provinces_id']];
        $st[]=['city_id','EQ',$user['city_id']];
        $st[]=['created_at','between time',[$start, $end]];
        $query = $this->_query($this->table)
                ->where($st)
                ->like('name')
                ->equal('id,name,tid,status,is_show');
        $query->order('id desc')->page(); 
       
    }

        /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {



        $this->clist = Db::name('MarketType')->field("id,name")->select();
        foreach ($data as &$vo) {

            $vo['marketstatus'] = TransItem::marketStatus(Arr::get($vo, 'status'));
            $vo['province'] = DB::name("Area")->where('area_id',$vo['provinces_id'])->value("area_name");
            $vo['city'] = DB::name("Area")->where('area_id',$vo['city_id'])->value("area_name");
            $vo['add_time'] = date("Y-m-d",$vo['created_at']);
            foreach ($this->clist as $cate) if ($cate['id'] === $vo['tid']) $vo['cate'] = $cate;

        }
    }

    /**
     * 二手日报表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function statdata(){

        $this->title="二手日报表";

        $user=$this->agentUser;
        $data[]=['provinces_id','EQ',$user['provinces_id']];
        $data[]=['city_id','EQ',$user['city_id']];

        $start_time= $this->request->param('start_time');
        $end_time=$this->request->param('end_time');

        if(!empty($start_time)&&!empty($end_time)){

            $start=strtotime($start_time." 00:00:00");
            $end=strtotime($end_time. " 23:59:59");
            if($start>$end){
                $this->error('开始时间应小于结束时间');
            }
            $data[]=['for_time','between time',[$start, $end]];

        }


        $query = $this->_query("AgencyMarketStat")
                ->where($data)
                ->where("type=1")
                ->like('id')
                ->equal('id,for_time');
        $query->order('id desc')->page(); 
    }

       /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _statdata_page_filter(&$data)
    {
        foreach ($data as &$vo) {

            $vo['add_time'] = date("Y年m月d日",$vo['for_time']);

        }
    }

    //二手日报表详情
    public function detail(){

        $id =$this->request->param('id');
        $total=DB::name("AgencyMarketStat")->where('id',$id)->find();
        $this->assign('total',$total);


        $st[]=['provinces_id','EQ',$total['provinces_id']];
        $st[]=['city_id','EQ',$total['city_id']];
        $st[]=['status','NEQ',0];
        $st[]=['created_at','between time',[$total['for_time_start'], $total['for_time_end']]];

        $query = $this->_query($this->table)
                ->where($st)
                ->like('name')
                ->equal('name');
        $query->order('id desc')->page();  

    }

     /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _detail_page_filter(&$data)
    {

        $this->clist = Db::name('MarketType')->field("id,name")->select();
        foreach ($data as &$vo) {

            $vo['marketstatus'] = TransItem::marketStatus(Arr::get($vo, 'status'));
            $vo['province'] = DB::name("Area")->where('area_id',$vo['provinces_id'])->value("area_name");
            $vo['city'] = DB::name("Area")->where('area_id',$vo['city_id'])->value("area_name");
            $vo['add_time'] = date("Y-m-d",$vo['created_at']);
            foreach ($this->clist as $cate) if ($cate['id'] === $vo['tid']) $vo['cate'] = $cate;

        }
    }


    /**
     * 二手月报表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function statmonth(){

        $this->title="二手月报表";

        $user=$this->agentUser;
        $data[]=['provinces_id','EQ',$user['provinces_id']];
        $data[]=['city_id','EQ',$user['city_id']];

        $start_time= $this->request->param('start_time');
 
        $end_time=$this->request->param('end_time');

        if(!empty($start_time)&&!empty($end_time)){

            $start=strtotime($start_time." 00:00:00");
            $end=strtotime($end_time. " 23:59:59");
            if($start>$end){
                $this->error('开始时间应小于结束时间');
            }
            $data[]=['for_time','between time',[$start, $end]];

        }

        $query = $this->_query("AgencyMarketStat")
                ->where($data)
                ->where("type=2")
                ->like('id')
                ->equal('id,for_time');
        $query->order('id desc')->page(); 
    }


        /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _statmonth_page_filter(&$data)
    {
        foreach ($data as &$vo) {

            $vo['add_time'] = date("Y年m月",$vo['for_time_end']);

        }
    }

    //二手月报表详情
    public function mdetail(){

        $id =$this->request->param('id');
        $total=DB::name("AgencyMarketStat")->where('id',$id)->find();
        $this->assign('total',$total);


        $st[]=['provinces_id','EQ',$total['provinces_id']];
        $st[]=['city_id','EQ',$total['city_id']];
        $st[]=['created_at','between time',[$total['for_time_start'], $total['for_time_end']]];

        $query = $this->_query($this->table)
                ->where($st)
                ->like('name')
                ->equal('name');
        $query->order('id desc')->page();  

    }

     /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _mdetail_page_filter(&$data)
    {

         $this->clist = Db::name('MarketType')->field("id,name")->select();
        foreach ($data as &$vo) {

            $vo['marketstatus'] = TransItem::marketStatus(Arr::get($vo, 'status'));
            $vo['province'] = DB::name("Area")->where('area_id',$vo['provinces_id'])->value("area_name");
            $vo['city'] = DB::name("Area")->where('area_id',$vo['city_id'])->value("area_name");
            $vo['add_time'] = date("Y-m-d",$vo['created_at']);
            foreach ($this->clist as $cate) if ($cate['id'] === $vo['tid']) $vo['cate'] = $cate;

        }
    }




  

}
